import java.util.Arrays;
import java.util.Scanner;

public class Bull {
    public static int[] number;
    public static int b = 0;
    public static int c = 0;
    public static Scanner scan = new Scanner(System.in);
    public static int[] nub = new int[4];
    public static boolean n = true;

    public static void generate() {
        number = new int[4];
        for (int i = 0; i < 4; i++) { // 找几次
            int num = (int) (Math.random() * 9); // 随机数
            boolean repeat = false; // 重复状态
            for (int j = 0; j < i; j++) {
                if (num == number[j]) { // 重复
                    repeat = true;
                }
            }
            if (repeat) { // 重复
                i--;
            } else { // 不重复 将不重复的值赋值
                number[i] = num;
            }
        }
        System.out.println(Arrays.toString(number));
    }

    public static void input() {
        boolean flag = true;
        System.out.println("输入四个号码");
        try {
            for (int i = 0; i < nub.length; i++) {
                System.out.println("请输入第" + (i + 1) + "个号码");
                nub[i] = scan.nextInt();
                if (nub[i] >= 0 && nub[i] < 10) {
                    for (int j = 0; j < i; j++) {
                        if (nub[i] == nub[j]) {
                            System.out.println("重复请重新输入");
                            i--;
                            break;
                        }
                    }
                } else {
                    System.out.println("请输入0-9的数字");
                }
            }
        } catch (Exception e) {
            System.out.println("非法输入 请重新输入");
        }
        contrast();
    }

    public static void contrast() {
        for (int i = 0; i < number.length; i++) {
            for (int j = 0; j < number.length; j++) {
                if (number[i] == nub[j]) {//数字相同
                    c++;
                }
            }
        }//随机的数字与用户的数字作比较 得出母牛的次数
        for (int i = 0; i < number.length; i++) {
            for (int j = 0; j < number.length; j++) {
                if (number[i] == nub[j] && i == j) {//数字和下标一致
                    b++;
                    c--;
                }
            }
            if (b == 4) {
                System.out.println("成功");
                n =false;
                break;
            }
        }//随机的数字与用户的数字作比较 得出公牛的次数
        System.out.println("公牛为：" + b + "母牛为：" + c);
    }

    public static void main(String[] args) {
        generate();
        while (n){
        for (int i = 0; i <= 5; i++) {
                    System.out.println("还有" + (5-i)  + "次机会");
                    input();
                    if(!n){
                        break;
                    }
                }
            }

    }
}

